package com.focusai.efairy.network;


import com.focusai.efairy.network.exception.CertificateException;
import com.focusai.efairy.network.exception.ConnectionException;
import com.focusai.efairy.network.exception.NetworkException;
import com.focusai.efairy.network.exception.TimeoutException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.security.cert.CertificateExpiredException;

/**
 * Created by heqingbao on 16/9/20.
 */
class WrapperException {

    private Exception mException;

    public WrapperException(Exception exception) {
        mException = exception;
    }

    public NetworkException unwrap() {
        if (mException instanceof UnknownHostException || mException instanceof ConnectException) {
            return new ConnectionException(mException);
        } else if (mException instanceof SocketTimeoutException) {
            return new TimeoutException(mException);
        } else if (mException instanceof java.security.cert.CertificateException) {
            CertificateException ex = new CertificateException(mException);
            if (mException instanceof CertificateExpiredException) {
                ex.setErrorCode(NetErrorCode.CERTIFICATE_EXPIRE_ERROR);
            } else {
                ex.setErrorCode(NetErrorCode.CERTIFICATE_ERROR);
            }
            return ex;
        } else {
            return new NetworkException(mException);
        }
    }
}
